package com.hcb.micro.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.dto.ProductSeller;
import com.hcb.micro.service.IMallService;
import com.hcb.micro.service.IProductSellerService;
import com.hcb.micro.service.IQqbsAccountService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("productSeller")
public class ProductSellerController extends BaseControllers{
	
	@Autowired
	IQqbsAccountService qqbsAccountService;
	
	@Autowired
	IMallService mallService;
	
	@Autowired
	IProductSellerService productSellerService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String list() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		
		if(manager.getType() == 3){
			para.put("productSellerId", manager.getProductSellerId());
		}
		
		if (bodyInfo.get("productSellerId") != null && !"".equals(bodyInfo.get("productSellerId"))) {
			para.put("productSellerId", bodyInfo.getString("productSellerId"));
		}
		if (bodyInfo.get("name") != null && !"".equals(bodyInfo.get("name"))) {
			para.put("name", bodyInfo.getString("name"));
		}
		
		List<ProductSeller> productSellerAllList = productSellerService.selectAllByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		List<ProductSeller> productSellerList = productSellerService.selectAllByPara(para);
		
		if(productSellerList.size() <= 0){
			json.put("result", "1");
			json.put("description", "供应商列表不存在");
			return buildReqJsonObject(json);
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String,Object>> list = new ArrayList<>();
		for(ProductSeller productSeller:productSellerList){
			Map<String,Object> map = new HashMap<>();
			
			map.put("id", productSeller.getId()+"");
			map.put("name", productSeller.getName()+"");
			map.put("company", productSeller.getCompany()==null?"":productSeller.getCompany());
			map.put("address", productSeller.getAddress()==null?"":productSeller.getAddress());
			map.put("undertakeCount", productSeller.getUndertakeCount()==null?"":productSeller.getUndertakeCount());
			map.put("logo", productSeller.getLogo()==null?"":productSeller.getLogo());
			map.put("status", productSeller.getStatus());
			map.put("createTime", productSeller.getCreateTime()==null?"":sdf.format(productSeller.getCreateTime()));
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取供应商列表成功");
		json.put("list", list);
		json.put("count", productSellerAllList.size());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "detail", method = RequestMethod.POST)
	@ResponseBody
	public String detail() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("productSellerId") == null || "".equals(bodyInfo.get("productSellerId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		ProductSeller productSeller = productSellerService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("productSellerId")));
		
		if(productSeller == null){
			json.put("result", "1");
			json.put("description", "供应商信息不存在");
			return buildReqJsonObject(json);
		}
		Map<String,Object> map = new HashMap<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		map.put("id", productSeller.getId()+"");
		map.put("name", productSeller.getName()+"");
		map.put("company", productSeller.getCompany()==null?"":productSeller.getCompany());
		map.put("address", productSeller.getAddress()==null?"":productSeller.getAddress());
		map.put("undertakeCount", productSeller.getUndertakeCount()==null?"":productSeller.getUndertakeCount());
		map.put("logo", productSeller.getLogo()==null?"":productSeller.getLogo());
		map.put("status", productSeller.getStatus());
		map.put("createTime", productSeller.getCreateTime()==null?"":sdf.format(productSeller.getCreateTime()));
		
    	json.put("result", "0");
		json.put("description", "获取供应商信息成功");
		json.put("productSeller", map);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("name") == null || "".equals(bodyInfo.get("name"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		ProductSeller productSeller = new ProductSeller();
		productSeller.setName(bodyInfo.getString("name"));
		if(bodyInfo.get("company") != null && !"".equals(bodyInfo.get("company"))){
			productSeller.setCompany(bodyInfo.getString("company"));
		}
		if(bodyInfo.get("address") != null && !"".equals(bodyInfo.get("address"))) {
			productSeller.setAddress(bodyInfo.getString("address"));
		}
		if(bodyInfo.get("logo") != null && !"".equals(bodyInfo.get("logo"))) {
			productSeller.setLogo(bodyInfo.getString("logo"));
		}
		
		if(manager.getType() != 3){
			if(bodyInfo.get("undertakeCount") != null && !"".equals(bodyInfo.get("undertakeCount"))) {
				productSeller.setUndertakeCount(Integer.valueOf(bodyInfo.getString("undertakeCount")));
			}
		}
		
		productSeller.setStatus((byte)1);
		productSeller.setCreateTime(new Date());
		Integer insert = productSellerService.insertSelective(productSeller);
		if(insert != 1){
			System.out.println("---新增供应商失败---");
			json.put("result", "0");
			json.put("description", "新增供应商失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "新增供应商成功");
		json.put("categoryId", productSeller.getId());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "modify", method = RequestMethod.POST)
	@ResponseBody
	public String modify() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("productSellerId") == null || "".equals(bodyInfo.get("productSellerId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		ProductSeller productSeller = productSellerService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("productSellerId")));
		if(productSeller == null){
			json.put("result", "0");
			json.put("description", "供应商信息不存在");
			return buildReqJsonObject(json);
		}
		if(bodyInfo.get("name") != null && !"".equals(bodyInfo.get("name"))){
			productSeller.setName(bodyInfo.getString("name"));
		}
		if(bodyInfo.get("company") != null && !"".equals(bodyInfo.get("company"))){
			productSeller.setCompany(bodyInfo.getString("company"));
		}
		if(bodyInfo.get("address") != null && !"".equals(bodyInfo.get("address"))) {
			productSeller.setAddress(bodyInfo.getString("address"));
		}
		if(bodyInfo.get("logo") != null && !"".equals(bodyInfo.get("logo"))) {
			productSeller.setLogo(bodyInfo.getString("logo"));
		}
		if(bodyInfo.get("status") != null && !"".equals(bodyInfo.get("status"))) {
			productSeller.setStatus(Byte.valueOf(bodyInfo.getString("status")));
		}
		
		if(manager.getType() != 3){
			if(bodyInfo.get("undertakeCount") != null && !"".equals(bodyInfo.get("undertakeCount"))) {
				productSeller.setUndertakeCount(Integer.valueOf(bodyInfo.getString("undertakeCount")));
			}
		}
		
		Integer update = productSellerService.updateByPrimaryKeySelective(productSeller);
		if(update != 1){
			System.out.println("---修改供应商失败---");
			json.put("result", "0");
			json.put("description", "修改供应商失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "修改供应商成功");
		json.put("categoryId", productSeller.getId());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("productSellerId") == null || "".equals(bodyInfo.get("productSellerId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		ProductSeller productSeller = productSellerService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("productSellerId")));
		if(productSeller == null){
			json.put("result", "0");
			json.put("description", "供应商信息不存在");
			return buildReqJsonObject(json);
		}
		Integer delete = productSellerService.deleteByPrimaryKey(productSeller.getId());
		if(delete != 1){
			System.out.println("---删除供应商失败---");
			json.put("result", "0");
			json.put("description", "删除供应商失败");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "删除供应商成功");
		return buildReqJsonObject(json);
	}
}
